
(function(d) {
    var b = mobiscroll,
        a = b.$,
        c = {
            wheelOrder: 'hhiiss',
            useShortLabels: false,
            min: 0,
            max: Infinity,
            labels: ['Years', 'Months', 'Days', 'Hours', 'Minutes', 'Seconds'],
            labelsShort: ['Yrs', 'Mths', 'Days', 'Hrs', 'Mins', 'Secs']
        };
    b.presetShort('timespan');
    b.presets.scroller.timespan = function(i) {
        function o(c) {
            var b = {};
            a(p).each(function(d, a) {
                b[a] = j[a] ? Math.floor(c / f[a].limit) : 0;
                c -= b[a] * f[a].limit;
            });
            return b;
        }

        function A(c) {
            var d = false,
                e = s[j[c] - 1] || 1,
                b = f[c],
                g = b.label,
                a = b.wheel;
            a.data = [];
            a.label = b.label;
            if (q.match(new RegExp(b.re + b.re, 'i'))) {
                d = true;
            }
            if (c == l) {
                a.min = n[c];
                a.max = m[c];
                a.data = function(a) {
                    return {
                        value: a,
                        display: w(a * e, d, g)
                    };
                };
                a.getIndex = function(a) {
                    return Math.round(a / e);
                };
            } else {
                for (k = 0; k <= b.until; k += e) {
                    a.data.push({
                        value: k,
                        display: w(k, d, g)
                    });
                }
            }
        }

        function w(a, b, c) {
            return (a < 10 && b ? '0' : '') + a + '<span class="mbsc-ts-lbl">' + c + '</span>';
        }

        function y(c) {
            var d = 0,
                b = 0;
            a.each(g, function(a, e) {
                if (!isNaN(+c[d])) {
                    b += f[e.v].limit * c[a];
                }
            });
            return b;
        }

        function z(b, a) {
            return Math.floor(b / a) * a;
        }
        var k, r, x, n, m, u = a.extend({}, i.settings),
            e = a.extend(i.settings, c, u),
            q = e.wheelOrder,
            h = e.useShortLabels ? e.labelsShort : e.labels,
            p = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'],
            f = {
                'years': {
                    ord: 0,
                    index: 6,
                    until: 10,
                    limit: 1000 * 60 * 60 * 24 * 365,
                    label: h[0],
                    re: 'y',
                    wheel: {}
                },
                'months': {
                    ord: 1,
                    index: 5,
                    until: 11,
                    limit: 1000 * 60 * 60 * 24 * 30,
                    label: h[1],
                    re: 'm',
                    wheel: {}
                },
                'days': {
                    ord: 2,
                    index: 4,
                    until: 31,
                    limit: 1000 * 60 * 60 * 24,
                    label: h[2],
                    re: 'd',
                    wheel: {}
                },
                'hours': {
                    ord: 3,
                    index: 3,
                    until: 23,
                    limit: 1000 * 60 * 60,
                    label: h[3],
                    re: 'h',
                    wheel: {}
                },
                'minutes': {
                    ord: 4,
                    index: 2,
                    until: 59,
                    limit: 1000 * 60,
                    label: h[4],
                    re: 'i',
                    wheel: {}
                },
                'seconds': {
                    ord: 5,
                    index: 1,
                    until: 59,
                    limit: 1000,
                    label: h[5],
                    re: 's',
                    wheel: {}
                }
            },
            g = [],
            s = e.steps || [],
            j = {},
            l = 'seconds',
            v = e.defaultValue || Math.max(e.min, Math.min(0, e.max)),
            t = [
                []
            ];
        a(p).each(function(b, a) {
            r = q.search(new RegExp(f[a].re, 'i'));
            if (r > -1) {
                g.push({
                    o: r,
                    v: a
                });
                if (f[a].index > f[l].index) {
                    l = a;
                }
            }
        });
        g.sort(function(a, b) {
            return a.o > b.o ? 1 : -1;
        });
        a.each(g, function(b, a) {
            j[a.v] = b + 1;
            t[0].push(f[a.v].wheel);
        });
        n = o(e.min);
        m = o(e.max);
        a.each(g, function(b, a) {
            A(a.v);
        });
        i.getVal = function(a, b) {
            return b ? i._getVal(a) : i._hasValue || a ? y(i.getArrayVal(a)) : null;
        };
        return {
            showLabel: true,
            wheels: t,
            compClass: 'mbsc-ts',
            parseValue: function(d) {
                var c = [],
                    h;
                if (b.util.isNumeric(d) || !d) {
                    x = o(d || v);
                    a.each(g, function(b, a) {
                        c.push(x[a.v]);
                    });
                } else {
                    a.each(g, function(b, a) {
                        h = new RegExp('(\\d+)\\s?(' + e.labels[f[a.v].ord] + '|' + e.labelsShort[f[a.v].ord] + ')', 'gi').exec(d);
                        c.push(h ? h[1] : 0);
                    });
                }
                a(c).each(function(a, b) {
                    c[a] = z(b, s[a] || 1);
                });
                return c;
            },
            formatValue: function(c) {
                var b = '';
                a.each(g, function(a, d) {
                    b += +c[a] ? c[a] + ' ' + f[d.v].label + ' ' : '';
                });
                return b ? b.replace(/\s+$/g, '') : 0;
            },
            validate: function(t) {
                var r, b, c, e, q = t.values,
                    s = t.direction,
                    k = [],
                    h = true,
                    g = true;
                a(p).each(function(t, p) {
                    if (j[p] !== d) {
                        c = j[p] - 1;
                        k[c] = [];
                        e = {};
                        if (p != l) {
                            if (h) {
                                for (b = m[p] + 1; b <= f[p].until; b++) {
                                    e[b] = true;
                                }
                            }
                            if (g) {
                                for (b = 0; b < n[p]; b++) {
                                    e[b] = true;
                                }
                            }
                        }
                        q[c] = i.getValidValue(c, q[c], s, e);
                        r = o(y(q));
                        h = h && r[p] == m[p];
                        g = g && r[p] == n[p];
                        a.each(e, function(a) {
                            k[c].push(a);
                        });
                    }
                });
                return {
                    disabled: k
                };
            }
        };
    };
}());